import {createReducer, on, Action} from '@ngrx/store';

import {PlayState} from '../../data-types/store/player-reducer.interface';
import * as Actions from '../actions/player.action';

export const initialState: PlayState = {
  playing: false,
  songList: [],
  playList: [],
  playMode: {
    type: 'loop',
    label: '循环'
  },
  currentIndex: -1
};

const reducer = createReducer(
  initialState,
  on(Actions.SetPlaying, (state, {playing}) => ({...state, playing})),
  on(Actions.SetPlayList, (state, {playList}) => ({...state, playList})),
  on(Actions.SetSongList, (state, {songList}) => ({...state, songList})),
  on(Actions.SetPlayMode, (state, {playMode}) => ({...state, playMode})),
  on(Actions.SetCurrentIndex, (state, {currentIndex}) => ({...state, currentIndex}))
);

export const playerReducer = (state: PlayState, action: Action) => {
  return reducer(state, action);
};
